﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.DBFHelper
{
    /// <summary>
    ///  上交所Show2003.dbf文件中的记录结构
    /// </summary>
    public class Show2003Record : DBFRecordBase<Show2003Record>
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        private Show2003Record() { }

        /// <summary>
        /// 从字节数组中读取DBF记录
        /// </summary>
        /// <param name="buffer">字节数组</param>
        /// <param name="offset">偏移</param>
        /// <returns></returns>
        public override DBFRecordBase ReadRecordFromBuffer(byte[] data, int offset, Dictionary<string, DbfColumn> columnDic)
        {

            Show2003Record rtn = new Show2003Record()
            {
                StopFlagStr = ReadStringField(data, offset, 0, 1),
                StkId = ReadStringField(data, offset, columnDic["S1"].Offset, columnDic["S1"].Size),
                StkName = ReadStringField(data, offset, columnDic["S2"].Offset, columnDic["S2"].Size),
                ClosePrice = ReadDoubleField(data, offset, columnDic["S3"].Offset, columnDic["S3"].Size),
                OpenPrice = ReadDoubleField(data, offset, columnDic["S4"].Offset, columnDic["S4"].Size),
                HighPrice = ReadDoubleField(data, offset, columnDic["S6"].Offset, columnDic["S6"].Size),
                LowPrice = ReadDoubleField(data, offset, columnDic["S7"].Offset, columnDic["S7"].Size),
                KnockPrice = ReadDoubleField(data, offset, columnDic["S8"].Offset, columnDic["S8"].Size),
                TotalMktKnockAmt = ReadDoubleField(data, offset, columnDic["S5"].Offset, columnDic["S5"].Size),
                TotalMktKnockQty = ReadInt64Field(data, offset, columnDic["S11"].Offset, columnDic["S11"].Size),
                PriceEarningRatio = ReadDoubleField(data, offset, columnDic["S13"].Offset, columnDic["S13"].Size),
                BuyQty1 = ReadIntField(data, offset, columnDic["S15"].Offset, columnDic["S15"].Size),
                BuyQty2 = ReadIntField(data, offset, columnDic["S17"].Offset, columnDic["S17"].Size),
                BuyQty3 = ReadIntField(data, offset, columnDic["S19"].Offset, columnDic["S19"].Size),
                BuyQty4 = ReadIntField(data, offset, columnDic["S27"].Offset, columnDic["S27"].Size),
                BuyQty5 = ReadIntField(data, offset, columnDic["S29"].Offset, columnDic["S29"].Size),
                BuyPrice1 = ReadDoubleField(data, offset, columnDic["S9"].Offset, columnDic["S9"].Size),
                BuyPrice2 = ReadDoubleField(data, offset, columnDic["S16"].Offset, columnDic["S16"].Size),
                BuyPrice3 = ReadDoubleField(data, offset, columnDic["S18"].Offset, columnDic["S18"].Size),
                BuyPrice4 = ReadDoubleField(data, offset, columnDic["S26"].Offset, columnDic["S26"].Size),
                BuyPrice5 = ReadDoubleField(data, offset, columnDic["S28"].Offset, columnDic["S28"].Size),
                SellQty1 = ReadIntField(data, offset, columnDic["S21"].Offset, columnDic["S21"].Size),
                SellQty2 = ReadIntField(data, offset, columnDic["S23"].Offset, columnDic["S23"].Size),
                SellQty3 = ReadIntField(data, offset, columnDic["S25"].Offset, columnDic["S25"].Size),
                SellQty4 = ReadIntField(data, offset, columnDic["S31"].Offset, columnDic["S31"].Size),
                SellQty5 = ReadIntField(data, offset, columnDic["S33"].Offset, columnDic["S33"].Size),
                SellPrice1 = ReadDoubleField(data, offset, columnDic["S10"].Offset, columnDic["S10"].Size),
                SellPrice2 = ReadDoubleField(data, offset, columnDic["S22"].Offset, columnDic["S22"].Size),
                SellPrice3 = ReadDoubleField(data, offset, columnDic["S24"].Offset, columnDic["S24"].Size),
                SellPrice4 = ReadDoubleField(data, offset, columnDic["S30"].Offset, columnDic["S30"].Size),
                SellPrice5 = ReadDoubleField(data, offset, columnDic["S32"].Offset, columnDic["S32"].Size),

            };
            return rtn;
        }


        #region 属性

        /// <summary>
        /// 删除（停牌）标志
        /// </summary>
        [DBFRecordField(0, 1, "")]
        private string StopFlagStr
        {
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    StopFlag = false;
                }
                else if (value[0] == 0x2a)
                {
                    StopFlag = true;
                }
                else
                {
                    StopFlag = false;
                }
            }
        }

        /// <summary>
        ///  删除（停牌）标志
        /// </summary>
        public bool StopFlag { get; private set; }


        /// <summary>
        /// 证券代码
        /// </summary>
        [DBFRecordField(1, 6, "S1")]
        public string StkId { get; private set; }

        /// <summary>
        /// 证券名称
        /// </summary>
        [DBFRecordField(7, 8, "S2")]
        public string StkName { get; private set; }

        /// <summary>
        /// 前收盘价格
        /// </summary>
        [DBFRecordField(15, 8, "S3")]
        public double ClosePrice { get; private set; }

        /// <summary>
        /// 今开盘价格
        /// </summary>
        [DBFRecordField(23, 8, "S4")]
        public double OpenPrice { get; private set; }

        /// <summary>
        /// 最高价格
        /// </summary>
        [DBFRecordField(43, 8, "S6")]
        public double HighPrice { get; private set; }

        /// <summary>
        /// 最低价格
        /// </summary>
        [DBFRecordField(51, 8, "S7")]
        public double LowPrice { get; private set; }

        /// <summary>
        /// 最新价格
        /// </summary>
        [DBFRecordField(59, 8, "S8")]
        public double KnockPrice { get; private set; }

        /// <summary>
        /// 今成交金额
        /// </summary>
        [DBFRecordField(31, 12, "S5")]
        public double TotalMktKnockAmt { get; private set; }

        /// <summary>
        /// 成交数量
        /// </summary>
        [DBFRecordField(83, 10, "S11")]
        public long TotalMktKnockQty { get; private set; }

        /// <summary>
        /// 市盈率
        /// </summary>
        [DBFRecordField(93, 8, "S13")]
        public double PriceEarningRatio { get; private set; }

        /// <summary>
        /// 申买量一
        /// </summary>
        [DBFRecordField(101, 10, "S15")]
        public int BuyQty1 { get; private set; }

        /// <summary>
        /// 申买量二
        /// </summary>
        [DBFRecordField(119, 10, "S17")]
        public int BuyQty2 { get; private set; }

        /// <summary>
        /// 申买量三
        /// </summary>
        [DBFRecordField(137, 10, "S19")]
        public int BuyQty3 { get; private set; }

        /// <summary>
        /// 申买量四
        /// </summary>
        [DBFRecordField(201, 10, "S27")]
        public int BuyQty4 { get; private set; }

        /// <summary>
        /// 申买量五
        /// </summary>
        [DBFRecordField(219, 10, "S29")]
        public int BuyQty5 { get; private set; }

        /// <summary>
        /// 当前买入价格
        /// </summary>
        [DBFRecordField(67, 8, "S9")]
        public double BuyPrice1 { get; private set; }

        /// <summary>
        /// 申买价二
        /// </summary>
        [DBFRecordField(111, 8, "S16")]
        public double BuyPrice2 { get; private set; }

        /// <summary>
        /// 申买价三
        /// </summary>
        [DBFRecordField(129, 8, "S18")]
        public double BuyPrice3 { get; private set; }

        /// <summary>
        /// 申买价四
        /// </summary>
        [DBFRecordField(193, 8, "S26")]
        public double BuyPrice4 { get; private set; }

        /// <summary>
        /// 申买价五
        /// </summary>
        [DBFRecordField(211, 8, "S28")]
        public double BuyPrice5 { get; private set; }

        /// <summary>
        /// 申卖量一
        /// </summary>
        [DBFRecordField(147, 10, "S21")]
        public int SellQty1 { get; private set; }

        /// <summary>
        /// 申卖量二
        /// </summary>
        [DBFRecordField(165, 10, "S23")]
        public int SellQty2 { get; private set; }

        /// <summary>
        /// 申卖量三
        /// </summary>
        [DBFRecordField(183, 10, "S25")]
        public int SellQty3 { get; private set; }

        /// <summary>
        /// 申卖量四
        /// </summary>
        [DBFRecordField(237, 10, "S31")]
        public int SellQty4 { get; private set; }

        /// <summary>
        /// 申卖量五
        /// </summary>
        [DBFRecordField(255, 10, "S33")]
        public int SellQty5 { get; private set; }

        /// <summary>
        /// 当前卖出价格
        /// </summary>
        [DBFRecordField(75, 8, "S10")]
        public double SellPrice1 { get; private set; }

        /// <summary>
        /// 申卖价二
        /// </summary>
        [DBFRecordField(157, 8, "S22")]
        public double SellPrice2 { get; private set; }

        /// <summary>
        /// 申卖价三
        /// </summary>
        [DBFRecordField(175, 8, "S24")]
        public double SellPrice3 { get; private set; }

        /// <summary>
        /// 申卖价四
        /// </summary>
        [DBFRecordField(229, 8, "S30")]
        public double SellPrice4 { get; private set; }

        /// <summary>
        /// 申卖价五
        /// </summary>
        [DBFRecordField(247, 8, "S32")]
        public double SellPrice5 { get; private set; }

        #endregion
        
    }
}
